﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EntGuayasSLA.BL.Archivos.Pdf;
using Ext.Net;
using EGuayas.Web.Resources;

namespace EGuayas.Web
{
    public partial class pgDetailPdf : pgBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            { 
               
                //Aqui consultar el link por code y pwd para leer el pdf
                string filePath = Request["idx"];
                string fileExt = filePath.Substring(filePath.LastIndexOf('.') + 1).ToLower();
                switch (fileExt)
                {
                    case "pdf":
                        byte[] OWNER = Encoding.UTF8.GetBytes(ConfigurationManager.AppSettings.Get("PwdPdf"));
                        using (MemoryStream memory = Pdf.DecryptFilePdf(MapPath( filePath),OWNER))
                        {   
                            Response.Clear();
                            Response.ContentType = "application/pdf";
                            Response.OutputStream.Write(memory.GetBuffer(), 0, memory.GetBuffer().Length);
               
                
                            Response.OutputStream.Close();
                            HttpContext.Current.ApplicationInstance.CompleteRequest();
                         }
                        break;
                    case "doc":
                    case "docx":
                        string urlNow = ConfigurationManager.AppSettings.Get("Url");
                          //string hostLink = 
                          string urlLink  = filePath.Replace(" ~", urlNow);
                          urlLink = urlLink.Replace("~", urlNow);
                        panelShow.Loader.Url =
                            string.Format("https://docs.google.com/viewer?url={0}&embedded=true", urlLink);
                          panelShow.Loader.Mode = LoadMode.Frame;
                          panelShow.LoadContent();
                          break;
                    default:
                          panelShow.Loader.Url = filePath;
                          panelShow.Loader.Mode = LoadMode.Frame;
                          panelShow.LoadContent();   
                        break;
                   
                }

              

            }
            catch (Exception ex)
            {               
                Response.Clear();
                StringBuilder sb = new StringBuilder();
                sb.Append("<div id='main-frame-error'>");
                sb.Append("<div id='box'>");
                sb.Append("<div class='read1'>");
                sb.Append("<div class='read1-img1'>");
                sb.Append("<img width='32' height='30' src='';   title='Archivo no encontrado.'>");
                sb.Append("</div>");
                sb.Append("<center><h3>Este recurso web no está disponible</h3></center>");
                sb.Append("<p>");
                sb.Append("<center>Es posible recurso web esté temporalmente inactivo o que se haya trasladado definitivamente a otra dirección.</center>");
                sb.Append("</p>");
                sb.Append("</div>");
                sb.Append("</div>");
                sb.Append("</div>");
                Response.Write(sb.ToString());
                RegistrarLog(ex);
            }

          
        }
    }
}